﻿using System;
using System.Linq;
using System.IO;
using System.IO.IsolatedStorage;
using System.Collections.Generic;
using Microsoft.LightSwitch;
using Microsoft.LightSwitch.Framework.Client;
using Microsoft.LightSwitch.Presentation;
using Microsoft.LightSwitch.Presentation.Extensions;

namespace LightSwitchApplication
{
    public partial class CreateNewAddress
    {
        partial void CreateNewAddress_InitializeDataWorkspace(List<IDataService> saveChangesTo)
        {
            // Write your code here.
            this.AddressProperty = new Address();
            if (this.AddressProperty != null && this.AddressProperty.CountryRegion == null)
                this.AddressProperty.CountryRegion = DataWorkspace.ApplicationData.CountryRegions.FirstOrDefault();

            if (this.ShopId != 0)
            {
                this.AddressProperty.Shop = this.DataWorkspace.ApplicationData.Shops.Where(e => e.Id == this.ShopId).FirstOrDefault();
            }
            if (this.CustomerId != 0)
            {
                this.AddressProperty.Customer = this.DataWorkspace.ApplicationData.Customers.Where(e => e.Id == this.CustomerId).FirstOrDefault();
            }
            if (this.ContactId != 0)
            {
                this.AddressProperty.Contact = this.DataWorkspace.ApplicationData.Contacts.Where(e => e.Id == this.ContactId).FirstOrDefault();
            }
            if (this.SalesPersonId != 0)
            {
                this.AddressProperty.SalesPerson = this.DataWorkspace.ApplicationData.SalesPersons.Where(e => e.Id == this.SalesPersonId).FirstOrDefault();
            }
        }

        partial void CreateNewAddress_Saved()
        {
            // Write your code here.
            this.Close(false);
            Application.Current.ShowAddressDetail(this.AddressProperty.Id);
        }

        partial void CreateNewAddress_Closing(ref bool cancel)
        {
            this.Close(cancel);
        }

        partial void Cancel_Execute()
        {
            this.Close(false);
        }
    }
}